// -------------------------- COMMON Variables
var map = null;
var nantesLatLng = new google.maps.LatLng(47.217703, -1.549416);
var draggableMarker = null;

var openedInfoWindow = null;
var layers = { "parkings" : { "markers" : [] } //
, "bicloos" : { "markers" : [] } //
, "transports" : { "markers" : [] } //
, "radars_fixes" : { "markers" : [] } //
, "radars_mobiles" : { "markers" : [] } //
, "radars_feux" : { "markers" : [] } //
, "covoiturage" : { "markers" : [] }
, "eventful" : { "markers" : [] } //
}; //
var settings = { "parkings" : { "isDisplayable" : false } //
, "bicloos" : { "isDisplayable" : false } //
, "bus" : { "isDisplayable" : false } //
, "tramway" : { "isDisplayable" : false } //
, "photos" : { "isDisplayable" : false } //
, "transports" : { "isDisplayable" : false } //
, "radars_fixes" : { "isDisplayable" : false } //
, "radars_mobiles" : { "isDisplayable" : false } //
, "radars_feux" : { "isDisplayable" : false } //
, "user" : { "isDisplayable" : true } //
, "covoiturage" : { "isDisplayable" : false } //
, "eventful" : { "isDisplayable" : false }  //
}; //
var tickers = new Array();
var tweets = new Array();

// -------------------------- GOOGLE MAPS INIT
function initialize() {
	// Request the map
	var myOptions = { zoom : 12, center : nantesLatLng,
		mapTypeId : google.maps.MapTypeId.ROADMAP };
	// Set the map into gui
	map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

	// Traffic overlay (not available in france for now)
	var trafficLayer = new google.maps.TrafficLayer();
	trafficLayer.setMap(map);

	// --- From search-autocomplete.js : Init autocomplete
	initSearchAutocomplete('searchTextField');

	// --- From public-equipment.js : Pre-load public equipments
	requestPublicEquipments();
	// --- From radars.js : Pre-load
	requestCovoiturage();
	// --- From weather.js : Get weather
	requestWeather();
	// --- From tan.js : Get TAN info
	requestTanRealTimeInfo();
	requestFuturesInfo();
	// --- From twitter.js : get tweets
	requestTweets();
	// --- From evenful.js : get events
	requestEventful();
	// --- From radars.js : Pre-load
	setTimeout(requestRadars, 1000);

	// Update the ticker && tweets after 1s
	setTimeout(updateTicker, 1000);
	setTimeout(updateTweets, 1000);
	
	// Geolocalisation
	getUserGeolocalisation();
	
	draggableMarker = new MarkerWithLabel({
	    draggable: true,
	    raiseOnDrag: true,
	    labelContent: "",
	    labelClass: "markerLabel",
	    labelAnchor: new google.maps.Point(0, 0)
	  });
	google.maps.event.addListener(draggableMarker, "drag", function (mEvent) { 
		draggableMarker.set('labelContent', mEvent.latLng.toString());
	});
}

// --------------------------- INIT TICKER
function updateTicker() {
	for ( var i = 0; i < tickers.length; i++) {
		var ticker = tickers[i];
		$("ul#ticker").append(
				"<li><span>" + ticker.date + "</span><a href='#'>"
						+ ticker.label + "</a></li>");
	}
	$("ul#ticker").liScroll({ travelocity : 0.10 });
}

// --------------------------- INIT DROPDOWN MENU
$(document).ready(function() {
	$("ul.subnav").parent().append("<span></span>");
	$("ul.topnav li span").hover(function() { 
		$(this).parent().find("ul.subnav").slideDown('fast').show();
		$(this).parent().hover(function() {
		}, function() {
			$(this).parent().find("ul.subnav").slideUp('slow');
		});
	}).hover(function() {
		$(this).addClass("subhover");
	}, function() { 
		$(this).removeClass("subhover");
	});

});

// --------------------------- CENTRER MAP ON NANTES
function showDraggableMarker(markerPosition, markerLabel) {
	draggableMarker.setPosition(markerPosition);
	draggableMarker.set('labelContent',markerLabel);
	draggableMarker.setMap(map);
}

// --------------------------- MANAGE LAYERS DISPLAY
function showLayer(menuButton, layerName) {
	if (layerName == 'parkings') {
		displayLayer(layerName);
	} else if (layerName == 'bicloos') {
		displayLayer(layerName)
	} else if (layerName == 'transports') {
		displayLayer(layerName)
	} else if (layerName == 'radars_fixes') {
		displayLayer(layerName)
	} else if (layerName == 'radars_mobiles') {
		displayLayer(layerName)
	} else if (layerName == 'radars_feux') {
		displayLayer(layerName)
	} else if (layerName == 'covoiturage') {
		displayLayer(layerName)
	}  else if (layerName == 'eventful') {
		displayLayer(layerName)
	} else if (layerName == 'photos') {
		requestParamanioLayer();
	} else if (layerName == 'tramway') {
		loadOrDisplayRoute(0);
	} else if (layerName == 'bus') {
		loadOrDisplayRoute(3);
	}
	if (layerName == 'user') {
		getUserGeolocalisation();
	} else  {
		setDisplayIconInMenu(settings[layerName]['isDisplayable'], menuButton);
	}
}

function displayLayer(layerName) {
	var isDisplayed = settings[layerName]['isDisplayable'];
	isDisplayed = !isDisplayed;
	for ( var i = 0; i < layers[layerName]['markers'].length; i++) {
		if (isDisplayed) {
			layers[layerName]['markers'][i]['marker'].setMap(map);
		} else {
			layers[layerName]['markers'][i]['marker'].setMap(null);
		}
	}
	settings[layerName]['isDisplayable'] = isDisplayed;
}

function setDisplayIconInMenu(displayed, menuButton) {
	if (displayed) {
		$(menuButton).parent().addClass('layer');
	} else {
		$(menuButton).parent().removeClass('layer');
	}
}

// --------------------------- DISPLAY ABOUT
function showAbout() {
	$("#about-dialog").toggle();
}

//--------------------------- SHOW MESSAGE
function showMessage(message) {
	$("#msg-dialog").removeClass("boxfadeout");
	$("#msg-dialog").html(message);
	$("#msg-dialog").addClass("boxfadein");
	$("#msg-dialog").show();
	setTimeout(function() {
		$("#msg-dialog").removeClass("boxfadein");
		$("#msg-dialog").addClass("boxfadeout");
		setTimeout(function() {
			$("#msg-dialog").hide();	
		}, 1000);
	}, 4000);
}


